// 统一路径前缀
const baseUrl = "http://112.124.14.110:80"

// const interceptor = {
//   config:{
//     baseURL: "http://112.124.14.110:80"
//   },
//   response: (res)=>{
//     console.log("执行了拦截器")
//     if(res.data.code !== 200){
//       wx.showToast({
//         title: 'res.data.message',
//         icon:'error',
//         duration:2000,
//         mask:true
//       })
//     }
//   }
// }

// App({
//   interceptor:interceptor
// })



// ------------------------User-------------------------------
/**
 * 发送验证码
 * @param {*} email 邮箱
 */
const sendCode = (email, success, fail) => {
  wx.request({
    url: baseUrl + '/users/code',
    method: 'GET',
    data: {
      email
    },
    success,
    fail
  })
}

/**
 * 用户注册
 * @param {*} username 用户名
 * @param {*} email 邮箱
 * @param {*} code 验证码
 * @param {*} password 密码
 */
const registerUser = (username, email, code, password, success, fail) => {
  wx.request({
    url: baseUrl + '/users/register',
    method: 'POST',
    data: {
      username,
      email,
      code,
      password
  },
    success,
    fail
  })
}

/**
 * 用户登陆
 * @param {*} account 账号
 * @param {*} password 密码
 */
const loginUser = (account, password, success, fail) => {
  wx.request({
    url: baseUrl + '/users/login',
    method: 'POST',
    data: {
      account,
      password
  },
    success,
    fail
  })
}

/**
 * 重置token
 * @param {*} token
 */
const resetLogin = (token, success, fail) => {
  wx.request({
    url: baseUrl + '/users/reset',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

/**
 * 修改用户名
 * @param {*} username 用户名
 * @param {*} token 
 */
const updateUsername = (username, token, success, fail) => {
  wx.request({
    url: baseUrl + '/users/name/' + username,
    method: 'PUT',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

/**
 * 更新用户头像
 * @param {*} filePath 图片上传临时路径
 * @param {*} token 
 */
const updateUserAvatar = (filePath, token, success, fail) => {
  wx.uploadFile({
    url: baseUrl + '/users/avatar',
    filePath,
    name: 'file',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

/**
 * 修改用户邮箱
 * @param {*} email 邮箱
 * @param {*} code 验证码
 * @param {*} token 
 */
const updateUserEmail = (email, code, token, success, fail) => {
  wx.request({
    url: baseUrl + '/users/email',
    method: 'PUT',
    header: {
      'Authorization': token
    },
    data: {
      email,
      code
    },
    success,
    fail
  })
}

/**
 * 修改用户密码
 * @param {*} password 密码
 * @param {*} code 验证码
 * @param {*} token 
 */
const updateUserPassword = (password, code, token, success, fail) => {
  wx.request({
    url: baseUrl + '/users/password',
    method: 'PUT',
    header: {
      'Authorization': token
    },
    data: {
      password,
      code
    },
    success,
    fail
  })
}

/**
 * 获取用户信息
 * @param {*} token 
 * @param {*} success 
 * @param {*} fail 
 */
const getUser = ( token,success,fail )=>
{
  wx.request({
    url: baseUrl + '/users/getUser',
    method:'GET',
    header:{
      'Authorization': token
    },
    success,
    fail
  })
}

const getFolloweds = (token,success,fail)=>{
  wx.request({
    url: baseUrl + '/users/getFolloweds',
    method:'GET',
    header:{
      'Authorization': token
    },
    success,
    fail
  })
}

const getFans = (token,success,fail)=>{
  wx.request({
    url: baseUrl + '/users/getFans',
    method:'GET',
    header:{
      'Authorization': token
    },
    success,
    fail
  })
}

const getUserById = (token, searchedId,success, fail) => {
  wx.request({
    url: baseUrl + '/users/getUserById/'+searchedId,
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const isLikePost = (token,postId,success,fail) => {
  wx.request({
    url: baseUrl + '/post/isLikePost',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      'postId': postId
    },
    success,
    fail
  })
}

const isfollow = (token, followedId,success, fail) => {
  wx.request({
    url: baseUrl + '/users/isfollow',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      followedId:followedId
    },
    success,
    fail
  })
}

const follow = (token,followedId, success, fail) => {
  wx.request({
    url: baseUrl + '/users/follow/'+followedId,
    method: 'POST',
    header: {
      'Authorization': token
    },
    data: {
      followedId
    },
    success,
    fail
  })
}

const unfollow = (token, followedId,success, fail) => {
  wx.request({
    url: baseUrl + '/users/unfollow',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      followedId:followedId
    },
    success,
    fail
  })
}

// ------------------------Note-------------------------------


const post = (token,type,postTitle,content,postLocation,success, fail) => {
  wx.request({
    url: baseUrl + '/post/post/',
    method: 'POST',
    header: {
      'Authorization': token
    },
    data: {
      type,
      postTitle,
      content,
      postLocation
    },
    success,
    fail
  })
}

const postImage = (token, postUrl0, file, success, fail) => {
  wx.uploadFile({
    url: baseUrl + '/post/postImage', // 替换为实际的后端 API URL
    filePath: file,
    name: 'file',
    method: 'POST',
    header: {
      'Authorization': token
    },
    formData: {
      postId: postUrl0
    },
    success,
    fail
  });
};

const postVideo=(token,postUrl0, file,success,fail)=>{
  wx.uploadFile({
    url: baseUrl + '/post/postVideo', // 替换为实际的后端 API URL
    filePath: file,
    name: 'file',
    method: 'POST',
    header: {
      'Authorization': token
    },
    formData: {
      postId: postUrl0
    },
    success,
    fail
  });
}

const likePost = (token,postId,success,fail) => {
  wx.request({
    url: baseUrl + '/post/likePost',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      'postId': postId
    },
    success,
    fail
  })
}

const disLikePost = (token,postId,success,fail) => {
  wx.request({
    url: baseUrl + '/post/disLikePost',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      'postId': postId
    },
    success,
    fail
  })
}


const getUserPost = (token, success, fail) => {
  wx.request({
    url: baseUrl + '/post/getUserPost',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const getPost = (token, postId,success, fail) => {
  wx.request({
    url: baseUrl + '/post/getPost/',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      postId:postId
    },
    success,
    fail
  })
}

const delPost = (token, postId,success, fail) => {
  wx.request({
    url: baseUrl + '/post/delPost/',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      postId
    },
    success,
    fail
  })
}

const getUserFollowedPosts = (token, success, fail) => {
  wx.request({
    url: baseUrl + '/post/getUserFollowedPosts',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const getTypePosts = (token,type,success,fail) => {
  wx.request({
    url: baseUrl + '/post/getTypePosts',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      'type': type
    },
    success,
    fail
  })
}

const getLikePosts = (token, success, fail) => {
  wx.request({
    url: baseUrl + '/post/getLikePosts',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const getPostByUserId = (token,post_owner_id,success,fail) => {
  wx.request({
    url: baseUrl + '/post/getPostByUserId',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      'post_owner_id': post_owner_id
    },
    success,
    fail
  })
}

const recommendedPost = (token,success, fail) => {
  wx.request({
    url: baseUrl + '/post/recommendedPost',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const recommendedVideo = (token,success, fail) => {
  wx.request({
    url: baseUrl + '/post/recommendedVideo',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

/**
 * 上传文件
 * @param {*} filePath 文件临时路径
 * @param {*} token 
 */
const uploadFileTemp = (filePath, token, success, fail) => {
  wx.uploadFile({
    url: baseUrl + '/basic',
    filePath,
    name: 'file',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const comment = (token,postId,content, success, fail) => {
  wx.request({
    url: baseUrl + '/review/comment',
    method: 'POST',
    header: {
      'Authorization': token
    },
    data: {
      postId,
      content
    },
    success,
    fail
  })
}

const getPostReview = (token, postId,success, fail) => {
  wx.request({
    url: baseUrl + '/review/getPostReview',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      postId:postId
    },
    success,
    fail
  })
}

const reply = (token,repliedReviewId,postId,content, success, fail) => {
  wx.request({
    url: baseUrl + '/review/reply',
    method: 'POST',
    header: {
      'Authorization': token
    },
    data: {
      repliedReviewId,
      postId,
      content
    },
    success,
    fail
  })
}

const disLikeReview = (token, reviewId,success, fail) => {
  wx.request({
    url: baseUrl + '/review/disLikeReview',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      reviewId:reviewId
    },
    success,
    fail
  })
}



const likeReview = (token, reviewId,success, fail) => {
  wx.request({
    url: baseUrl + '/review/likeReview',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      reviewId:reviewId
    },
    success,
    fail
  })
}



const delReview = (token, reviewId,success, fail) => {
  wx.request({
    url: baseUrl + '/review/delReview',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      reviewId:reviewId
    },
    success,
    fail
  })
}

const isLikeReview = (token, reviewId,success, fail) => {
  wx.request({
    url: baseUrl + '/review/isLikeReview',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      reviewId:reviewId
    },
    success,
    fail
  })
}

const getNewPostReview = (token,success, fail) => {
  wx.request({
    url: baseUrl + '/message/getNewPostReview',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const getNewFans = (token,success, fail) => {
  wx.request({
    url: baseUrl + '/message/getNewFans',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const getNewLiked = (token,success, fail) => {
  wx.request({
    url: baseUrl + '/message/getNewLiked',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const clearLikedMessage = (token,success, fail) => {
  wx.request({
    url: baseUrl + '/message/clearLikedMessage',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const clearReviewMessage = (token,success, fail) => {
  wx.request({
    url: baseUrl + '/message/clearReviewMessage',
    method: 'GET',
    header: {
      'Authorization': token
    },
    success,
    fail
  })
}

const searchPostsByTitle = (token,str,success, fail) => {
  wx.request({
    url: baseUrl + '/post/searchPostsByTitle',
    method: 'GET',
    header: {
      'Authorization': token
    },
    data: {
      str:str
    },
    success,
    fail
  })
}

/**
 * 
 */

module.exports = {  
  getNewPostReview,
  getNewFans,
  getNewLiked,
  clearReviewMessage,
  clearLikedMessage,
  searchPostsByTitle,

  sendCode,
  uploadFileTemp,

  registerUser,
  loginUser,
  resetLogin,
  getUser,
  getFolloweds,
  getFans,
  getUserById,

  isfollow,
  unfollow,
  follow,

  updateUsername,
  updateUserAvatar,
  updateUserEmail,
  updateUserPassword,

  post,
  postImage,
  postVideo,
  getUserPost,
  getPost,
  delPost,
  getUserFollowedPosts,
  getTypePosts,
  isLikePost,
  likePost,
  disLikePost,
  getLikePosts,
  getPostByUserId,

  comment,
  isLikeReview,
  likeReview,
  delReview,
  disLikeReview,
  reply,
  getPostReview,

  recommendedPost,
  recommendedVideo
}